/******************************************************************************************
* Function: add_to_list
* Description : Populate the DS
* Date: Nov 14th '10
* Author: Aditya Kr Verma
********************************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MYPORT 4500;
#define NO_OF_PARTS 1000;

void append_file_part(node **, char *, int , int ,int);
void append_list ( node **, char * );

/****************************************Data Structures ***********************************/


struct seeder{
	int seeder_id;
	struct sockaddr_in6 sd_sin;
}

struct leecher{
	int leecher_id;
	struct sockaddr_in6 lc_sin;
}

struct file_part{

        struct file_part *next;

	int part_num;	
        FILE *data_ptr;	// or simply int ptr	
	struct seeder seeder_list[MAX_SEEDS];    //list of seeders.
	struct leecher leecher_list[MAX_LEECHES];	//
	float percent_downloaded;
	enum status{SEEDING,COMPLETE,LEECHING,PENDING}download;
		
}*head,*curr,*tail;

struct node
	{
	 char *value;
	 struct node *next;
         struct file_part *node_part;
	};


/******************************Functions definations*************************************************/

void append_list ( node ** new, char * name )
{
    struct node *temp, *r ;
      
	  if ( *head == NULL )  // if the list is empty, create first node
      {
            temp = (node *)malloc ( sizeof ( struct node) ) ;
	    temp -> value = name ;
            temp -> next = NULL ;
            temp -> node_part = NULL;
            *new = temp ;
						
      }
      else
      {   		  
	temp = *new ;
	while ( temp -> next != NULL ) // go to last node 
	      temp = temp -> next ;
	
	r = (Node *)malloc ( sizeof ( Node ) ) ; // add node at the end 
	r -> value = name ;
	r -> next = NULL ;
        r -> node_part = NULL
	temp -> next = r ;
      } 
}

void append_file_part(node **new, char *file_name, int id, int myport,int part_number)
{
   struct node *temp,*new_temp, *r;
   char * fname;
   temp = *new ;
	while ( temp -> next != NULL ) // go to last node of file_name list
        {
          fname = temp->value;
		  if( strcmp((char *)file_name, (const char*)fname))==0)
		  {
			    if( temp->node_part = NULL)
			    {
			      
			        new_temp = (node *)malloc ( sizeof ( struct file_part) ) ;

                                new_temp-> next = NULL;
                                                          
                               	new_temp-> part_num = part_number;
	
				//FILE *data_ptr;	// or simply int ptr	

                                new_temp-> node_part-> seeder_list-> seeder_id = ;
				new_temp-> node_part-> seeder_list-> sd_sin.s_port = htons(MYPORT) ;
                                new_temp-> node_part-> seeder_list-> sd_sin.sin_addr.s_addr = INADDR_ANY ;
                                new_temp-> node_part-> seeder_list-> sd_sin.sin_family = AF_INET ;

                                //new_temp-> node_part-> leecher_list-> leecher_id = ;
				//new_temp-> node_part-> leecher_list-> lc_sin    =  ;
				
				//float percent_downloaded;
				//enum status{SEEDING,COMPLETE,LEECHING,PENDING}download;
                               
                              temp->node_part = new_temp;
     			     }

			    else
			    {
                                new_temp = temp->node_part ;

				while ( new_temp -> next != NULL ) 
				  new_temp = new_temp -> next ;

			        r = (Node *)malloc ( sizeof ( struct file_part ) ) 
                                r-> next = NULL;
                                r-> part_num = part_number;

				//FILE *data_ptr;	// or simply int ptr	

                                r-> node_part-> seeder_list-> seeder_id = ;
				r-> node_part-> seeder_list-> sd_sin.s_port = htons(MYPORT) ;
                                r-> node_part-> seeder_list-> sd_sin.sin_addr.s_addr = INADDR_ANY ;
                                r-> node_part-> seeder_list-> sd_sin.sin_family = AF_INET ;

                                //r-> node_part-> leecher_list-> leecher_id = ;
				//r-> node_part-> leecher_list-> lc_sin    =  ;
				
				//float percent_downloaded;
				//enum status{SEEDING,COMPLETE,LEECHING,PENDING}download;
                               
                                new_temp-> next = r;

			    }
		  }
		  else
		      temp = temp->next;
         }
}


/************************************************* main() ****************************/

int main()
{

	int i;
	struct node *head;
	head = 0;
	append_list(&head,name);
	char *file_name;
	int seeder_id = 101;

	append_list(&head,"MI2");
	append_list(&head,"MI3");
	append_list(&head,"MI4");

	printf("\nEnter the name of the torrent for download");
	scanf(" %s" file_name);

	for (i=1;i<=NO_OF_PARTS;i++)
	append_file_part(&head,file_name, seeder_id, MYPORT,i);

}


